using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace InfraStructureLayer
{
    public class WiseSerialization
    {
        public static string ToXml(object objToXml, bool includeNameSpace)
        {
            if (objToXml == null)
            {
                return "";
            }
            StreamWriter stWriter = null;

            XmlSerializer xmlserializer;
            string buffer;
            try
            {
                xmlserializer = new XmlSerializer(objToXml.GetType());
                MemoryStream memStream = new MemoryStream();
                stWriter = new StreamWriter(memStream);
                if (!includeNameSpace)
                {
                    System.Xml.Serialization.XmlSerializerNamespaces xs = new XmlSerializerNamespaces();
                    xs.Add("", "");
                    xmlserializer.Serialize(stWriter, objToXml, xs);
                }
                else
                {
                    xmlserializer.Serialize(stWriter, objToXml);
                }
                buffer = Encoding.GetEncoding("iso-8859-9").GetString(memStream.GetBuffer());
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            finally
            {
                if (stWriter != null)
                    stWriter.Close();
            }
            return buffer;
        }
        public static object XmlTo(string xmlString, Type type)
        {
            if (xmlString == "")
            {
                return null;
            }
            XmlSerializer xmlserializer;
            MemoryStream memStream = null;
            try
            {
                xmlserializer = new XmlSerializer(type);
                byte[] bytes = new byte[xmlString.Length];
                Encoding.GetEncoding("iso-8859-9").GetBytes(xmlString, 0, xmlString.Length, bytes, 0);
                memStream = new MemoryStream(bytes);
                object objectFromXml = xmlserializer.Deserialize(memStream);
                return objectFromXml;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            finally
            {
                if (memStream != null)
                    memStream.Close();
            }
        }

        public static byte[]  ToBinary(object objToXml)
        {
            MemoryStream xStream = new MemoryStream();

            BinaryFormatter xFormatter = new BinaryFormatter();

            xFormatter.Serialize(xStream, objToXml);
            return xStream.ToArray();
        }
        public static object BinaryTo(byte[] str)
        {
            MemoryStream xStream = new MemoryStream(str);
            BinaryFormatter xFormatter = new BinaryFormatter();

            object xTemp = xFormatter.Deserialize(xStream);

            return xTemp;
        }
    }
}
